package com.itheima.stock.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.itheima.stock.entity.StockBlockRtInfo;
import com.itheima.stock.entity.StockBusiness;

import com.itheima.stock.entity.StockMarketIndexInfo;
import com.itheima.stock.entity.StockMarketLogPrice;
import com.itheima.stock.mapper.StockBlockRtInfoMapper;
import com.itheima.stock.mapper.StockBusinessMapper;
import com.itheima.stock.mapper.StockMarketIndexInfoMapper;
import com.itheima.stock.mapper.StockMarketLogPriceMapper;
import com.itheima.stock.properties.MarketIndexProperties;
import com.itheima.stock.service.StockService;
import com.itheima.stock.util.DateTimeUtil;
import com.itheima.stock.vo.BlockRtVO;
import com.itheima.stock.vo.MarketIndexVO;
import com.itheima.stock.vo.StockUpdownVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;


/**
 * @author zby
 * @created 2024-06-23 16:34
 * @description  股票业务实现类
 */
@Service
public class StockServiceImpl implements StockService {

    private final StockBlockRtInfoMapper blockRtInfoMapper;

    private final StockMarketIndexInfoMapper marketIndexInfoMapper;

    private final StockMarketLogPriceMapper marketLogPriceMapper;


    private final MarketIndexProperties marketIndexProperties;
    private final StockBusinessMapper stockBusinessMapper;

    public StockServiceImpl(StockBusinessMapper stockBusinessMapper, MarketIndexProperties marketIndexProperties, StockMarketIndexInfoMapper marketIndexInfoMapper, StockMarketLogPriceMapper marketLogPriceMapper, StockBlockRtInfoMapper blockRtInfoMapper) {
        this.stockBusinessMapper = stockBusinessMapper;
        this.marketIndexProperties = marketIndexProperties;
        this.marketIndexInfoMapper = marketIndexInfoMapper;
        this.marketLogPriceMapper = marketLogPriceMapper;
        this.blockRtInfoMapper = blockRtInfoMapper;
    }


    @Override
    public List<StockBusiness> getStockBusiness() {

        return stockBusinessMapper.selectAll();
    }

    @Override
    public List<MarketIndexVO> innerIndexAll() {

        List<String> ids = marketIndexProperties.getInner();

//       LocalDateTime now = DateTimeUtil.getLastDateTime4Stock(LocalDateTime.now());

        String timeStr = "2021-12-26 10:56:00";

        // 定义格式模式（必须与字符串完全匹配）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 转换为LocalDateTime
        LocalDateTime last = LocalDateTime.parse(timeStr, formatter);

        //todo 模拟数据
        String mockDate = "20211226105600";

        LocalDateTime.parse(mockDate, DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));

        //todo mock data


        List<StockMarketIndexInfo> stockMarketIndexInfoList = marketIndexInfoMapper.selectByIdsAndTime(ids, last);


        List<StockMarketLogPrice> stockMarketLogPriceList = marketLogPriceMapper.selectByIdsAndDate(ids, last.toLocalDate());


        return   stockMarketIndexInfoList.stream().map((info) -> {

            MarketIndexVO vo = MarketIndexVO.builder().build();

            vo.setCode(info.getMarkId());
            vo.setName(info.getMarkName());
            vo.setCurDate(info.getCurTime().toLocalDate().toString());
            vo.setTradeAmt(info.getTradeAccount());
            vo.setTradePrice(info.getCurrentPrice());
            vo.setTradeVol(info.getTradeVolume());
            vo.setUpDown(info.getUpdownRate());
            stockMarketLogPriceList.forEach((StockMarketLogPrice logPrice) -> {

                if (info.getMarkId().equals(logPrice.getMarketCode())) {
                    vo.setOpenPrice(logPrice.getOpenPrice());
                    vo.setPreClosePrice(logPrice.getPreClosePrice());

                } else {
                    vo.setOpenPrice(BigDecimal.ZERO);
                    vo.setPreClosePrice(BigDecimal.ZERO);
                }
            });

            return vo;

        }).toList();


    }

    @Override
    public List<BlockRtVO> innerRtAll() {

        List<BlockRtVO> rtInfos = blockRtInfoMapper.selectAll();
        rtInfos.forEach((rtInfo) -> {
            rtInfo.setCurDate(LocalDateTime.now().toString());
        });



        if (CollectionUtil.isEmpty(rtInfos)){
            throw new RuntimeException("rtInfos is empty");
        }


        return rtInfos;
    }

    @Override
    public List<StockUpdownVO> stockIncreaseLimit() {
        String timeStr = "2021-12-26 10:56:00";

        // 定义格式模式（必须与字符串完全匹配）
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        // 转换为LocalDateTime
        LocalDateTime last = LocalDateTime.parse(timeStr, formatter);

       List<StockUpdownVO>  vo = marketIndexInfoMapper.selectUpDownLimit(last);

        if (CollectionUtil.isEmpty(vo)){
            throw new RuntimeException("rtInfos is empty");
        }


        return vo;
    }


}
